/**
 * Copyright (C) 2022 - 长期
 * All rights reserved.
 * <p>
 * 版权所有 (C) 中国烟草总公司
 */
package com.itzhoun.config.shiro;

import com.itzhoun.model.pojo.MangerInfo;
import com.itzhoun.model.pojo.SysRole;
import com.itzhoun.model.pojo.rcdb.Permission;
import com.itzhoun.service.UserService;
import com.itzhoun.util.JWTUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * @author zhounan
 * @date 2024/01/22 15:15
 * @Description
 */
public class MyShiroRealm extends AuthorizingRealm {

    @Resource
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(MyShiroRealm.class);

    /**
     * JWT签名密钥
     */
    public static final String SECRET = "f64e70b9e1b06bfbb9e1f6bf209465e5";

    @Override
    public boolean supports(AuthenticationToken token) {
        return true;
    }


    /**
     *
     * @author zhounan
     * @Create 2024/1/22 21:11
     * @Description 权限配置
     * @param principalCollection
     * @return org.apache.shiro.authz.AuthorizationInfo
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.info("权限配置 ---> MyShiro.doGetAuthorizationInfo()");
        String userName = JWTUtil.getUserName(principalCollection.toString());
        //下面的可以使用缓存提升速度
        MangerInfo mangerInfo = userService.findUserByUsername(userName);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //设置相应角色的权限信息
        for (SysRole sysRole : mangerInfo.getSysRoles()) {
            simpleAuthorizationInfo.addRole(sysRole.getRole());
            for (Permission permission : sysRole.getPermissions()) {
                //设置权限
                simpleAuthorizationInfo.addStringPermission(permission.getPermission());
            }
        }
        return simpleAuthorizationInfo;
    }

    /**
     *
     * @author zhounan
     * @Create 2024/1/22 15:28
     * @Description 认证信息(身份验证)
     * @param authenticationToken token信息
     * @return org.apache.shiro.authz.AuthorizationInfo
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        logger.info("MyShiro.doGetAuthorizationInfo()");
        String token = (String) authenticationToken.getCredentials();
        String userName = JWTUtil.getUserName(token);
        if (StringUtils.isBlank(userName)){
            throw new AuthenticationException("token invalid");
        }

        //从数据库去查username
        MangerInfo mangerInfo = userService.findUserByUsername(userName);

        if (mangerInfo == null){
            throw new AuthenticationException("USER NOT EXIST");
        }

        if (!JWTUtil.verify(token, userName, SECRET)){
            throw new AuthenticationException("token check fail");
        }
        return new SimpleAuthenticationInfo(token, token, "my_realm");
    }
}
